<!-- 
	获取微信头像和昵称并更新
	<c-wx-update-avatar-and-nickname v-if="visible" v-model:visible="visible"></c-wx-update-avatar-and-nickname>
-->
<template>
	<view class="avatar-and-nickname">
		<view class="avatar-and-nickname-content" :style="[safeAreaBottom]">
			<view class="panel">
				<!-- 关闭按钮 -->
				<button class="close-btn iconfont" type="default" @click="handleClose()">&#xe603;</button>
				<text class="tips-title">获取您的昵称、头像</text>
				<text class="tips-label">获取用户头像、昵称，主要用于向用户提供具有辨识度的用户中心界面</text>

				<!-- 列表 -->
				<view class="list">
					<view class="list-item show-icon">
						<text class="label">头像</text>
						<image class="avatar" :src="avatar" mode="aspectFill"></image>
						<!-- 选择头像按钮 -->
						<button class="choose-avatar-btn" type="default" open-type="chooseAvatar"
							@chooseavatar="handleChooseAvatar"></button>
					</view>
					<view class="list-item">
						<text class="label">昵称</text>
						<input class="input" :value="formData.nickname" type="nickname" placeholder="请输入昵称"
							@blur="handleChooseNickname" />
					</view>
				</view>

				<button class="submit-btn" :class="{'active': sumbitBtnCanUse}" type="default"
					:disabled="!sumbitBtnCanUse" @click="handleSubmit()">保存</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import api from "@/api/index.js";
	import { computed, ref, watchEffect } from 'vue';
	import { storeToRefs } from 'pinia';
	import { useUserStore } from '@/store/user.js'

	const props = defineProps({
		visible: false,
		close: false,
	});
	const emit = defineEmits(['update:visible']);

	// 手机底部安全距离
	const safeAreaBottom = { 'padding-bottom': getApp().globalData.safeAreaBottom + 'px' };

	const userStore = useUserStore();
	const { userInfo } = storeToRefs(userStore);
	const formData = ref({
		avatarId: '',
		avatar: '',
		nickname: '',
	});

	// 显示的头像
	const avatar = computed(() => {
		return formData.value.avatar ||
			'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132';
	});
	// 提交按钮能否使用
	const sumbitBtnCanUse = computed(() => {
		return formData.value.avatar && formData.value.nickname;
	});

	watchEffect(() => {
		formData.value.avatar = userInfo.value.path;
		formData.value.nickname = userInfo.value.nickname;
	});

	// 点击关闭
	const handleClose = () => {
		emit('update:visible', false);
	}

	// 选择头像后
	const handleChooseAvatar = async (res) => {
		uni.showLoading({ title: '上传中...' });
		const result = await api.user_api.uploadAvatar({
			filePath: res.detail.avatarUrl,
		}).finally(() => setTimeout(uni.hideLoading, 100));
		
		formData.value.avatar = result.data.data.path;
		formData.value.avatarId = result.data.data.id;
	}

	// 选择或输入昵称后
	const handleChooseNickname = (res) => {
		formData.value.nickname = res.detail.value;
	}

	// 保存
	const handleSubmit = async () => {
		if (/^https:\/\/thirdwx.qlogo.cn/.test(formData.value.avatar) || !formData.value.avatar) {
			uni.showToast({
				title: '请选择头像',
				icon: 'none',
			});
			return;
		}
		if (!formData.value.nickname) {
			uni.showToast({
				title: '请输入昵称',
				icon: 'none',
			});
			return;
		}

		const allUpdate = [];

		if (formData.value.avatarId) {
			allUpdate.push(updateAvatar(formData.value.avatarId));
		}
		if (formData.value.nickname !== userInfo.value.nickname) {
			allUpdate.push(updateNickname(formData.value.nickname));
		}

		try {
			await Promise.all(allUpdate);
			userStore.getUserInfo();
			emit('update:visible', false);
		} catch (err) {

		}
	}

	// 修改头像
	const updateAvatar = (id) => {
		return api.user_api.setAvatar(id);
	}

	// 修改昵称
	const updateNickname = (nickname) => {
		return api.user_api.setNickname({
			nickname
		});
	}
</script>

<style lang="less" scoped>
	@mainColor: #F44A43;

	.avatar-and-nickname {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);

		&-content {
			position: absolute;
			left: 0;
			bottom: 0;
			width: 100%;
			background-color: white;
			border-radius: 40rpx 40rpx 0 0;
		}

		.panel {
			padding: 20rpx 40rpx 30rpx;
			box-sizing: border-box;

			.close-btn {
				display: block;
				width: 40rpx;
				height: 40rpx;
				font-size: 40rpx;
				color: #878C9E;
				line-height: 40rpx;
				background-color: transparent;
				padding: 0;
				margin: 0 0 0 auto;
			}

			.tips-title {
				display: block;
				font-size: 30rpx;
				color: #262626;
				font-weight: bold;
				margin-top: 10rpx;
			}

			.tips-label {
				display: block;
				font-size: 26rpx;
				color: #878C9E;
				margin-top: 15rpx;
			}

			.list {
				margin-top: 30rpx;

				&-item {
					position: relative;
					display: flex;
					align-items: center;
					height: 100rpx;
					border-top: 1rpx solid #ECEDEF;

					&:last-child {
						border-bottom: 1rpx solid #ECEDEF;
					}

					&.show-icon::after {
						position: absolute;
						top: 50%;
						right: 10rpx;
						transform: translateY(-50%);
						font-size: 30rpx;
						color: #878C9E;
						font-family: iconfont;
						content: '\e792';
					}

					.label {
						font-size: 30rpx;
						color: #262626;
						padding-right: 30rpx;
					}

					.avatar {
						width: 60rpx;
						height: 60rpx;
						border-radius: 6rpx;
					}

					.input {
						flex-grow: 1;
						font-size: 30rpx;
						color: #262626;
					}

					.choose-avatar-btn {
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						background: transparent;
					}
				}
			}

			.submit-btn {
				width: 300rpx;
				font-size: 30rpx;
				color: #878C9E;
				background-color: #F2F2F2;
				border-radius: 4rpx;
				margin: 40rpx auto 0;
				padding: 14rpx 0;

				&.active {
					color: white;
					background-color: @mainColor;
				}
			}
		}
	}
</style>